/*
 * @Author: zxh
 * @Email: 1271027008@qq.com
 * @Date: 2020-06-01 17:14:19
 * @Descripttion: 
 */ 
import { Injectable, } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { RoleManageService } from '../role-manage/role-manage.service';
import { DepartmentManageService } from '../department-manage/department-manage.service';
import { encrypt } from "../../../assets/js/rsaEncrypt.js";

@Injectable({
    providedIn: 'root'
})
export class UserManageService {

    constructor(
        private http: HttpClient,
        private roleService: RoleManageService,
        private deptService: DepartmentManageService
    ) { }

    // 获取左侧树状结构
    private baseUrl = '/api/users';

    // 获取右侧表格
    getDepartMember(data): Observable<any> {
        let dataJson = {
            params: {
                ...data,
                page: data.page - 1,
            }
        }
        return this.http.get(this.baseUrl, dataJson)
    }
    // 新增用户
    addMember(data): Observable<any> {
        let newRoles = [];
        data.rolesId.forEach(w => {
            newRoles.push({ id: w });
        });
        let _dataJson = {
            ...data,
            password:encrypt(data.userPassword),
            dept: { id: data.deptId },
            //roles: [{ id: data.rolesId }]
            roles: newRoles
        }
        return this.http.post(this.baseUrl, _dataJson, {})
    }
    // 修改用户
    putMember(data): Observable<any> {
        let newRoles = [];
        data.rolesId.forEach(dt => {
            newRoles.push({ id: dt });
        });
        let resources = {
            ...data,
            dept: { id: data.deptId },
            password:encrypt(data.userPassword),
            // roles: [{ id: data.rolesId }]
            roles: newRoles
        }
        console.log(resources);
        return this.http.put(this.baseUrl, resources, {})
    }
    // 删除用户
    deleteMember(data): Observable<any> {
        let options = {
            headers: {
                "Content-Type": "application/json"
            },
            body: data
        }
        return this.http.delete(this.baseUrl, options)
    }
    // 角色列表
    getRoleList(): Observable<any> {
        return this.roleService.getRolesAll()
    }
    // 部门列表
    getDepartNodes(name): Observable<any> {
        return this.deptService.apiList({ name: name })
    }
    // 导出数据
    private downloadUrl = '/api/users/download'
    async downMember(data): Promise<Node> {
        let dataJson = {
            blurry: data.blurry,
            createTime: data.createTime,
            deptId: data.deptId,
            enabled: data.enabled
        }
        return new Promise<Node>((resolve, reject) => {
            this.http.get(this.downloadUrl, { params: dataJson }).subscribe((res: any) => {
                resolve(res);
            }, err => {
                reject(err)
            })
        })
    }
    //zxh 获取用户树
    private userTreeUrl = '/api/users/tree'
    getUserTree(): Observable<any>{
        return this.http.get(this.userTreeUrl)
    }

}
